package db

import (
	_ "github.com/go-sql-driver/mysql"
	"github.com/jinzhu/gorm"
	_ "github.com/jmoiron/sqlx"
)

var (
	DB  *gorm.DB
	err error
)

func InitDb() error {
	driverName := "mysql"
	dsn := "root:123.@tcp(127.0.0.1:3306)/blogs?parseTime=true&charset=utf8mb4&loc=Local"
	if DB, err = gorm.Open(driverName, dsn); err != nil {
		return err //此error只是判断上述参数是否正确，并不代表真的连接成功
	}
	if err = DB.DB().Ping(); err != nil {
		return err
	}
	DB.DB().SetMaxIdleConns(16)
	DB.DB().SetMaxOpenConns(100)
	//DB.AutoMigrate(&model.ArticleDetail{})
	//DB.AutoMigrate(&model.Comment{})
	//defer DB.Close()
	return nil

}
